var express = require('express');
var router = express.Router();
var Order = require("../models/orders.js");
var moment = require('moment');
var getTable = require("./getTable.js");

moment.locale('zh-cn');


router.get('/getOrders', function(req, res, next) {
    let page = parseInt(req.param("page"));
    let pageSize = parseInt(req.param("pageSize"));
    // let sort = req.param("sort");
    let skip = (page-1)*pageSize;
    let params = {};

    if (req.param("type") != ""){
        if(req.param("type") == "待付款"){
            params.ifPay=false;
        }else if (req.param("type") == "待发货"){
            params.ifPay=true;
            params.ifSend=false;
            params.ifGet=false;
        }else if (req.param("type") == "已发货"){
            params.ifPay=true;
            params.ifSend=true;
            params.ifGet=false;
        }else if (req.param("type") == "已完成"){
            params.ifPay=true;
            params.ifSend=true;
            params.ifGet=true;
        }else if (req.param("type") == "取消订单"){
            params.ifCancel=true;
        }

    }

    console.log(params)
    let model = Order.find(params).skip(skip).limit(pageSize);
    // goodsModel.sort({"salePrice" : sort});
    //var model = Goodslist.find();
    Order.count(params, (err, count) => {
        if(err){
            res.json({
                status:"1",
                msg:err.message
            })
        }else {
            model.exec(function (err1, result) {
                if (err1) {
                    res.json({
                        status: "1",
                        msg: err.message
                    })
                } else {
                    res.json({
                        status: "0",
                        msg: "",
                        result: {
                            count: count,
                            list: result
                        }
                    })
                }
            });
        }
    });
});

//修改价钱
router.post('/editTotal', function(req, res, next) {

    Order.update({
        _id:req.body.id
    },{
        $set:{'totalMoney':req.body.total}
    },function(err,doc){
        if(err){
            res.json({
                status:"1",
                msg:err.message,
                result: ""
            })
        }else{
            if(doc){
                res.json({
                    status:"0",
                    msg:"修改成功",
                    result: "suc"
                })
            }
        }
    });
});


//添加物流
router.post('/editExpress', function(req, res, next) {
    let logistics = {
        company : req.body.company,
        oddnum : req.body.oddnum,
        courier : req.body.courier,
       // phone : req.body.phone
    }
    Order.update({
        _id:req.body.id
    },{
        $set:{'logistics':logistics,"ifSend":true}
    },function(err,doc){
        if(err){
            res.json({
                status:"1",
                msg:err.message,
                result: ""
            })
        }else{
            if(doc){
                res.json({
                    status:"0",
                    msg:"修改成功",
                    result: "suc"
                })
            }
        }
    });
});


//确认收货
router.post('/confirmReceipt', function(req, res, next) {
    Order.update({
        _id:req.body.id
    },{
        $set:{"ifGet":true}
    },function(err,doc){
        if(err){
            res.json({
                status:"1",
                msg:err.message,
                result: ""
            })
        }else{
            if(doc){
                res.json({
                    status:"0",
                    msg:"修改成功",
                    result: "suc"
                })
            }
        }
    });
});

//营业额统计
router.get('/getTotle', function(req, res, next) {
    // let model = Order.find();
    let date = moment().subtract(1, 'days').format("YYYY-MM-DD");
    console.log(date);
    Order.find({"ifPay":true,"ifCancel":false},function (err1, result) {
        if (err1) {
            res.json({
                status: "1",
                msg: err.message
            })
        } else {
            let totalMoney = 0;
            let yettotalMoney = 0;

            result.forEach((item)=>{
                 totalMoney += item.totalMoney;
            })

            Order.find({"ifPay":true,"ifCancel":false, "$and":[{"date":{"$gte":date + " 0:0:0"}},{"date":{"$lte":date + " 23:59:59"}}]},function (err2,result2) {
                if (err2) {
                    res.json({
                        status: "1",
                        msg: err.message
                    })
                } else {
                    result2.forEach((item) => {
                        yettotalMoney += parseFloat(item.totalMoney);
                    })
                    res.json({
                        status: "0",
                        msg: "",
                        result: {
                            totalMoney: totalMoney,
                            yettotalMoney: yettotalMoney
                        }
                    })
                }
            })
        }
    });
})

//付款订单
router.get('/getPay', function(req, res, next) {
    // let model = Order.find();
    let date = moment().subtract(1, 'days').format("YYYY-MM-DD");
    Order.find({"ifPay":true,"ifCancel":false},function (err1, result) {
        if (err1) {
            res.json({
                status: "1",
                msg: err.message
            })
        } else {
            Order.find({"ifPay":true,"ifCancel":false,"$and":[{"date":{"$gte":date + " 0:0:0"}},{"date":{"$lte":date + " 23:59:59"}}]},function (err2,result2) {
                if (err2) {
                    res.json({
                        status: "1",
                        msg: err.message
                    })
                } else {
                    res.json({
                        status: "0",
                        msg: "",
                        result: {
                            allpay: result.length,
                            yetpay: result2.length
                        }
                    })
                }
            })
        }
    });
});

function data() {

}

function jisuan(data){
    let totalMoney = 0;
    if(data.length > 0){
        data.forEach((item)=>{
            totalMoney += item.totalMoney;
        })
        return totalMoney
    }
    return 0
}
//月表格
router.get('/getTable',async function(req, res, next) {

    let month = req.param("month");

    let days = req.param("month").split("-");
    let day = new Date(days[0],days[1],0);
    let monthlastday = day.getDate();
    let nowmonth = moment().format("MM");

    let seriesdata=[];
    let paydata=[];
    console.log(nowmonth)

    if (days[1] == nowmonth){
        let nowday = moment().format("DD");
        console.log(nowday)
        if(nowday >= 1 ) {
            let one = await getTable(month + "-01", month + "-06");
            console.log(one)
            seriesdata.push(jisuan(one));
            paydata.push(one.length);
        }
        if(nowday > 7){
            let two =  await getTable(month + "-07" ,month + "-13");
            console.log(two)
            seriesdata.push(jisuan(two));
            paydata.push(two.length);
        }
        else if(nowday > 14){
            let three =  await getTable(month + "-14" ,month + "-20");
            console.log(three)

            seriesdata.push(jisuan(three))
            paydata.push(three.length)
        }
        else if(nowday > 21){
            let four =  await getTable(month + "-21" ,month + "-27");
            seriesdata.push(jisuan(four))
            paydata.push(four.length)
        }
        if(nowday >= 28 ){
            let five =  await getTable(month + "-28" ,month + "-" + monthlastday);
            seriesdata.push(jisuan(five))
            paydata.push(five.length)
        }
    }else{

    let one =  await getTable(month + "-01" ,month + "-06");
    let two =  await getTable(month + "-07" ,month + "-13");
    let three =  await getTable(month + "-14" ,month + "-20");
    let four =  await getTable(month + "-21" ,month + "-27");
    let five =  await getTable(month + "-28" ,month + "-" + monthlastday);

     seriesdata = [jisuan(one),jisuan(two),jisuan(three),jisuan(four),jisuan(five)];
     paydata = [one.length,two.length,three.length,four.length,five.length];

    }
    res.json({
        status: "0",
        msg: "",
        result: {
            xAxisdata: [month + "-01-"+month + "-06",month + "-07-"+month + "-13",month + "-14-"+month + "-20",month + "-21-"+month + "-27",month + "-28-"+ month+"-"+monthlastday],
            seriesdata: seriesdata,
            paydata:paydata
        }
    })
    // console.log(jisuan(one))
    // console.log(jisuan(two))
    // console.log(jisuan(three))
    // console.log(jisuan(four))
    // console.log(jisuan(five))
});

module.exports = router;